package com.feng.fengDUtil.dao;

import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.function.Predicate;

public final class ResultSetHandlerHelp {

    private static Predicate[] predicates = {
            n->n==String.class,
            n->n==Integer.TYPE||n==Integer.class,
            n->n==Long.TYPE||n==Long.class,
            n->n==Double.TYPE||n==Double.class,
            n->n==Byte.TYPE||n==Byte.class,
            n->n==BigDecimal.class,
            n->n==Date.class,
            n->n==Boolean.TYPE||n==Boolean.class,
            n->n==Float.TYPE||n==Float.class,
    };

    private static boolean isBasicClass(Class clazz) {
        return Arrays.stream(predicates).anyMatch(n->n.test(clazz));
    }

    public static ResultSetHandler getResultSetHandler(boolean many, Class clazz) {
        if(clazz == Map.class){
            return many?new MapListHandler():new MapHandler();
        }else if(isBasicClass(clazz)){
            return many?new ColumnListHandler():new ScalarHandler();
        }else {
            // return new SingleListHandler();
            return many?new BeanListHandler(clazz):new BeanHandler(clazz);
        }
    }

}
